<UserControl x:Class="LockCrypt.DatabaseSelector"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:conv="clr-namespace:LockCrypt.Converters" x:Name="root"
    xmlns:lex="http://schemas.root-project.org/xaml/presentation"
    xmlns:l49ui="clr-namespace:Lime49.UI;assembly=Lime49"
    xmlns:l49loc="clr-namespace:Lime49.UI"
    FocusManager.IsFocusScope="True"
    FocusManager.FocusedElement="{Binding ElementName=cboDbMode}"
    Loaded="UserControl_Loaded">
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
        <conv:DatabaseModeIsFileBased x:Key="DatabaseModeIsFileBased"/>
        <conv:StringEqualityToInvertedBooleanConverter x:Key="StringEqualityToInvertedBooleanConverter"/>
    </UserControl.Resources>
    <StackPanel DataContext="{Binding}">
        <GroupBox Header="{lex:LocText LockCrypt.Core:Strings:DatabaseLocation}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="labels"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Content="{lex:LocText LockCrypt.Core:Strings:DatabaseMode}"/>
                <ComboBox x:Name="cboDbMode" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" SelectedValue="{Binding ElementName=root,Path=DatabaseMode,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                          DisplayMemberPath="Value" SelectedValuePath="Key" SelectionChanged="cboDbMode_SelectionChanged" TabIndex="1"/>
                <l49loc:FilePicker Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" x:Name="dbPathChooser" Visibility="{Binding SelectedValue,ElementName=cboDbMode,Mode=OneWay,Converter={StaticResource DatabaseModeIsFileBased}}"
                                LabelText="{lex:LocText LockCrypt.Core:Strings:DatabasePath}"
                                SelectedPath="{Binding ElementName=root,Path=DatabasePath,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                PathHistoryKey="{Binding ElementName=root,Path=DatabasePathHistoryKey,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                PathChanged="dbPathSelector_PathChanged"
                                IsSaveDialog="{Binding ElementName=root,Path=IsSaveDialog,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                DialogTitle="{lex:LocText LockCrypt.Core:Strings:ChooseFile}"/>
                <Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Visibility="{Binding SelectedValue,ElementName=cboDbMode,Mode=OneWay,Converter={StaticResource DatabaseModeIsFileBased},ConverterParameter=inverted}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="labels"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    
                    <Label Grid.Row="0" Grid.Column="0" Content="{lex:LocText LockCrypt.Core:Strings:DatabaseHost}"/>
                    <TextBox x:Name="txtDBHost" Grid.Row="0" Grid.Column="1" Text="{Binding ElementName=root,Path=DatabaseHost,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TabIndex="3" />
                    <Label Grid.Row="1" Grid.Column="0" Content="{lex:LocText LockCrypt.Core:Strings:DatabasePort}"/>
                    <l49ui:MaskedTextBox x:Name="txtDBPort" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"
                                       Type="Digits" Width="100" Text="{Binding ElementName=root,Path=DatabasePortString,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TabIndex="4" />
                    <Label Grid.Row="2" Grid.Column="0" Content="{lex:LocText LockCrypt.Core:Strings:DatabaseUser}"/>
                    <TextBox x:Name="txtDBUser" Grid.Row="2" Grid.Column="1" Text="{Binding ElementName=root,Path=DatabaseUser,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TabIndex="5" />
                    <Label Grid.Row="3" Grid.Column="0" Content="{lex:LocText LockCrypt.Core:Strings:DatabasePassword}"/>
                    <!--TextBox x:Name="txtDBPassword" Grid.Row="3" Grid.Column="1" Text="{Binding ElementName=root,Path=DatabasePassword,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /-->
                    <PasswordBox x:Name="txtDBPassword" Grid.Row="3" Grid.Column="1" TabIndex="6" />
                    <Label Grid.Row="4" Grid.Column="0" Content="{lex:LocText LockCrypt.Core:Strings:DatabaseName}"/>
                    <TextBox x:Name="txtDBName" Grid.Row="4" Grid.Column="1" Text="{Binding ElementName=root,Path=DatabaseName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TabIndex="7" />
                    <Label Grid.Row="5" Grid.Column="0" Content="{lex:LocText LockCrypt.Core:Strings:TablePrefix}" Visibility="Collapsed"/>
                    <TextBox x:Name="txtDBTablePrefix" Grid.Row="5" Grid.Column="1" Text="{Binding ElementName=root,Path=DatabaseTablePrefix,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Visibility="Collapsed" TabIndex="8"/>
                </Grid>
            </Grid>
        </GroupBox>
        <GroupBox Header="{lex:LocText LockCrypt.Core:Strings:Encryption}" 
                  IsEnabled="{Binding ElementName=root,Path=CanEditEncryption,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  Visibility="{Binding ElementName=root,Path=ShowEncryption,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BoolToVisibilityConverter}}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="labels"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <Label Grid.Row="0" Grid.Column="0" Content="{lex:LocText LockCrypt.Core:Strings:EncryptionAlgorithm}"/>
                <ComboBox Grid.Row="0" Grid.Column="1" x:Name="cboEncryption" TabIndex="9" DisplayMemberPath="Value" SelectedValuePath="Key" SelectionChanged="ccboEncryption_SelectionChanged"
                                      SelectedValue="{Binding ElementName=root,Path=EncryptionAlgorithm,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />

                <Label Grid.Row="1" Grid.Column="0" Content="{lex:LocText LockCrypt.Core:Strings:Password}"/>
                <PasswordBox x:Name="txtPassword" Grid.Row="1" Grid.Column="1" IsEnabled="{Binding SelectedValue,ElementName=cboEncryption,Converter={StaticResource StringEqualityToInvertedBooleanConverter},ConverterParameter='None'}" TabIndex="10" />

                <Label Grid.Row="2" Grid.Column="0" Content="{lex:LocText LockCrypt.Core:Strings:ConfirmPassword}"
                       Visibility="{Binding ElementName=root,Path=ShowPasswordConfirmation,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BoolToVisibilityConverter}}"/>
                <PasswordBox x:Name="txtConfirmPassword" Grid.Row="2" Grid.Column="1" IsEnabled="{Binding SelectedValue,ElementName=cboEncryption,Converter={StaticResource StringEqualityToInvertedBooleanConverter},ConverterParameter='None'}" TabIndex="11" 
                             Visibility="{Binding ElementName=root,Path=ShowPasswordConfirmation,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BoolToVisibilityConverter}}"/>
            </Grid>
        </GroupBox>
    </StackPanel>
</UserControl>
